// pages/share/share.js
let that,
  api = new(require('../../utils/api.js')),
  util = new(require('../../utils/util.js')),
  //imghead = api.getimgurl(),
  imghead = 'https://jianaijia.com/pdlapi/imgs/',
  videohead='https://jianaijia.com/api/myvideo/',
  audiohead='https://jianaijia.com/api/myaudio/',
  myaudio=wx.createInnerAudioContext(),
  app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    videohead,
    imghead,
    start:0, 
    top: [
      // {
      //   name: '热门',
      // },
      {
        name: '最新',
      },
      // {
      //   name: '关注',
      // }, 
      {
        name: '我的',
      },
    ],

    clickgood: !0,
    isCollect: false,
    currentTab: 0,
    currentageindex: 1,
    pagesize: 20,
    currentpage: 1,
    list: [],
    needupdate: !1,
    hideadd: !0,
    commontarr: [],
    isfollow:'',
    // followidsarr:[],
    hasphone:!1,
    sendshow:!1,
    isplay:!1,
    audioid:0,
    showRegister:!1,
    kindname:'',//幼儿园名称
    classname:'',//班级名称
    babyname:'',//宝宝姓名
  },

 
 /**getPhoneNumber
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this;
    if (app.logininfo) {
      // let logininfo = app.logininfo;
       that.getlist();
            } else {
              util.setwatcher(app,that.afterlogin)
            }
  },


  afterlogin: {
    logininfo: res => {
      let logininfo = app.logininfo
      that.getlist()
      // if (logininfo.user.nickName==null) {
      //   wx.navigateTo({
      //     url: '../userInfo/userInfo',
      //   })
      // } else {
      //   that.setData({
      //     hideadd: !1
      //   });
      // }
      that.setData({ myid: logininfo.userid,user:logininfo.user})
      console.log(app.logininfo,'logininfo');
      console.log('afterlogin fen');
     // that.getfavorlist();
    }
  },
 

  getlist() { 
    let { pagesize,start,id } = this.data
   // subdata = {start,pagesize,id};
    api.report_getall( res => {
      if (res.data.issuccess == 1) {
        let {list} = res.data
        list.forEach(x=>{
          x['updateDate'] = util.formatTime(new Date(x["item"].updateDate))
          x['username'] = x["item"]['username']
          x['words'] = x["item"]['words']
          if(x["item"].imgurls){
            x['imgs'] = x["item"].imgurls.split(',')
          }
          if(x.list.length>0){
            x.list.forEach(y=>{
              y['imgs'] = y.imgurls.split(',')
            })
          }
         // x['pics']= x['pics'].concat(x.imgurl.split(','))
          
        })
        this.setData({
          total: res.data.total,
          list:res.data.list
        });
          
      //  this.formatdata(res.data.list);
      } else {
      }
    });
  },

  formatdata(clist) {
    let {
      favorlist,
      list,followidsarr
    } = this.data, piclist = []; 
    clist.forEach((y, j) => {
      clist[j]["ellipsis"] = !1;
      clist[j]["favor"] = clist[j].item["favor"];
      clist[j].item["username"] = clist[j].item["username"] || '匿名用户' + clist[j].item.userid;
      clist[j]["isshow"] = !1;
      if(y.item.contents){
        clist[j]["wordcount"] = y.item.contents.length;
      }
      if (favorlist.indexOf(String(y.item.id)) != -1) {
        clist[j]["isfavor"] = !0;
      } else {
        clist[j]["isfavor"] = !1;
      }
    })
    clist.forEach((x, i) => {
      x.item.updateDate = util.formatTime(new Date(x.item.updateTime))
      x['isplay']=!1
      if (x.item.piclist!=null&&x.item.piclist!='') {
        piclist = x.item.piclist.split(',')
        piclist.forEach((y, i) => {
          piclist[i] = y.replace('imgs/','')
        });
        x.piclist = piclist;
      } 
      list.push(x)
    });
    this.setData({
      list
    },()=>{
      that.getheight()
    });
  },
  getheight(){
    let {list}=that.data
    let query = wx.createSelectorQuery().in(this);
    query.selectAll('.content').boundingClientRect().exec(res => {
      console.log(res, 'resres')
      let lineHeight = 25; //固定高度值 单位：PX
      for (var i = 0; i < res[0].length; i++) {
        if ((res[0][i].height / lineHeight) > 3) {
          console.log(res[0][i].height ,'res[0][i].height ');
          list[i]["ellipsis"] = !0;
          list[i]["isshow"] = !0
        }
      }
      that.setData({
        list
      })
    });
  },
  toadd(){
    let params='reportadd'
    wx.navigateTo({
      url: `../${params}/${params}?id=`+that.data.id,
    })
  },
  tabtap(e) {
    this.setData({
      list: []
    })
    let {index} = e.currentTarget.dataset, { currentageindex} = this.data;
    if (index == currentageindex) {
      return;
    }
    this.setData({
      currentageindex: index,
      currentpage: 1,
    })
    this.getlist(index, 0);
  },
  delcom(e) {
    let {
      id,
      index
    } = e.target.dataset, {
      list
    } = this.data;
    wx.showModal({
      title: '提示',
      content: '确定删除吗？',
      success: function(res) {
        if (res.confirm) {
          api.delcomment(id, res => {
            if (res.data.issuccess == 1) {
              console.log(res, "成功删除");
              list.splice(index, 1);
              that.setData({
                list
              });
            }
          })
        }
      }
    })
  },
  toGood(e) {
    console.log(e,'toGood')
    // this.setData({
    //   clickgood: !1
    // });
    // let {favor,id,isfavor,index} = e.currentTarget.dataset, { list } = this.data,title=isfavor?'取消点赞':'点赞成功';
    // favor = Number(favor);
    // console.log(list[index],'list[index]');
    // list[index]['isfavor'] = isfavor ? !1 : !0;
    // list[index]['favor'] = isfavor ? favor - 1 : favor + 1;
    // api.dofavor(id, res => {
    //   if (res.data.issuccess == 1) {
    //     that.setData({list})
    //     wx.showToast({
    //       title,
    //     })
    //   }
    // })
    // setTimeout(function() {
    //   that.setData({
    //     clickgood: !0
    //   })
    // }, 2000);

    let {id,islike} = e.currentTarget.dataset ,{currentageindex,list}=that.data
    if(islike=='0'){
      api.dofavor(id,res=>{
        if (res.data.issuccess == 1) {
          list.forEach(x=>{
            if(x.item.id==id){
              x['islike']=1
              x['item']['favor']=x.item.favor+1
            }
          })

          that.setData({list})
          // that.getlist(currentageindex, 0) 
          wx.showToast({
            title:'点赞成功',
            icon:'none'
          })
       }
      })
    }else{
      api.dofavor(id,res=>{
        if (res.data.issuccess == 1) {
          list.forEach(x=>{
            if(x.item.id==id){
              x['islike']=0
              x['item']['favor']=x.item.favor-1
              // x['favor']=x.favor-1
            }
          })

          that.setData({list})
          // that.getlist(currentageindex, 0) 
          wx.showToast({
            title:'取消成功',
            icon:'none'
          })
        }
      })
    }

  },
  previewimg(e) {
    let {
      imghead
    } = this.data, {
      src,
      arr,
      img
    } = e.currentTarget.dataset;
    let myarr=[];
    // arr.forEach((v,i)=>{
    //   myarr.push(imghead+v)
    // })
    console.log(myarr,'myarr');
    wx.previewImage({
      urls: [src],
      current: src
    })
  },
  getdata() {

    api.mp_getlist({
      start: 0,
      pagesize: 10
    }, res => {
      console.log(res, 'mp_getlist')
    })
  },
  inputtap(e){
    let {prop}=e.target.dataset,{value}=e.detail
    that.setData({[prop]:value})
  },
  register(){
    let {kindname,classname,babyname,KindergartenData} = that.data,err=''
    if(babyname=='') err='请输入宝宝姓名'
    if(classname=='') err='请输入班级名称'
    if(kindname=='') err='请输入幼儿园名称'
    if(err.length>0){
      wx.showToast({
        title: err,
        icon:'none'
      })
      return
    }
    wx.showLoading({
      title: '登记中...',
    })
    api.aoeKindergarten({kindname,classname,babyname},res=>{
      wx.hideLoading()
      if(res.data.issuccess==1){
        wx.showModal({
          title: '提示',
          content:"登记成功",
          showCancel:!1,
          success(res){
            if(res.confirm){
              api.getKindergarten(res=>{
                if(res.data.issuccess==1){
                  KindergartenData=res.data.data
                  that.setData({KindergartenData})
                }
              })
            }
          }
        })
        that.setData({showRegister:!1})
      }
    })
  },
  /**
   * 跳转评论详情页
   */
  skip(e) {
    console.log(111,'11')
    wx.navigateTo({
      url: '../reportadd/reportadd?type=c&id='+e.currentTarget.dataset.id,
    })
  //  let  {isgetphone}=that.data
  //   // that.setData({sendshow:!0})
  //   if(!isgetphone){
  //     that.setData({sendshow:!0})
  //   }else{
     
  //   }
  },
  no(){
    that.setData({sendshow:!1})
  },
  tolisten(){
    console.log(22222);
    wx.switchTab({
      url: '../answer/answer',
    })
  },
  tobank(){
    wx.navigateTo({
      url: '../days/days',
    })
  },
  showRegister(){
    wx.navigateTo({
      url: '../registration/registration',
    })
    // that.setData({showRegister:!0})
  },
  closeRegister(){
    that.setData({showRegister:!1})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let isgetphone=wx.getStorageSync('isgetphone')
    if(isgetphone){
      that.setData({isgetphone,hasphone:!0,sendshow:!1})
    }
    if (this.data.needupdate) {
      let {
        currentageindex
      } = this.data;
      that.setData({
        currentpage: 1,
        list: [],
        needupdate: !1,
        currentageindex:1,
      },()=>{
        
        that.getlist();
      });
    }

  },
  todetail(e){
    let {cid,cuserid,id}=e.currentTarget.dataset
    wx.navigateTo({
      url: '../sampleReelsDetail/sampleReelsDetail?cid='+cid+'&cuserid='+cuserid+'&id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // let {
    //   pagesize,
    //   currentpage,
    //   start,
    //   length,
    //   currentageindex
    // } = this.data;
    // if (length != pagesize) {
    //   wx.showToast({
    //     icon: "none",
    //     title: '再怎么扯也没有了',
    //   })
    //   return;
    // }
    // start = pagesize * currentpage;
    // currentpage = currentpage + 1;
    // this.setData({
    //   start,
    //   currentpage
    // });
    // this.getlist(currentageindex, start);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: app.logininfo.appletname,
      path: `pages/share/share?share=`+app.logininfo.userid,
    }
  }
})